package com.bugmonkey.cameraviewx.watermark;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bugmonkey.cameraviewx.ConstraintUtil;
import com.bugmonkey.cameraviewx.R;


public class WaterMarkParentView extends ConstraintLayout {
    private ImageView imageView;
    private WaterMarkContainerLayout container;


    public WaterMarkParentView(@NonNull Context context){
       super(context);
    }

    public WaterMarkParentView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0, 0);
    }
    public WaterMarkParentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public WaterMarkParentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView(){

        if (getId() == 0){
            setId(generateViewId());
        }
        setBackgroundColor(Color.BLACK);

        imageView = new ImageView(this.getContext());
        imageView.setId(ImageView.generateViewId());
//        imageView.setImageResource(R.mipmap.test);
//        imageView.setBackgroundColor(Color.RED);
        addView(imageView);

        ConstraintUtil.ConstraintBegin begin = new ConstraintUtil(this).begin();
        begin.clear(imageView.getId());
        begin.Left_toLeftOf(imageView.getId(),getId());
        begin.Right_toRightOf(imageView.getId(),getId());
        begin.Top_toTopOf(imageView.getId(),getId());
        begin.Bottom_toBottomOf(imageView.getId(),getId());

        begin.commit();




    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        container = findViewById(R.id.watermark_container);

        if(container != null){

            container.setBackgroundColor(Color.TRANSPARENT);
            ConstraintUtil.ConstraintBegin containerBegin = new ConstraintUtil(this).begin();
            containerBegin.clear(R.id.watermark_container);
            containerBegin.Left_toLeftOf(container.getId(),imageView.getId());
            containerBegin.Right_toRightOf(container.getId(),imageView.getId());
            containerBegin.Top_toTopOf(container.getId(),imageView.getId());
            containerBegin.Bottom_toBottomOf(container.getId(),imageView.getId());
            containerBegin.commit();
        }

    }

    /**
     * 设置图片
     * @param bitmap
     */
    public void setImageViewBitmap(Bitmap bitmap){

        imageView.setImageBitmap(bitmap);

        String ratio  = bitmap.getWidth()+":"+bitmap.getHeight();

        ConstraintUtil.ConstraintBegin begin = new ConstraintUtil(this).begin();

        begin.Left_toLeftOf(imageView.getId(),getId());
        begin.Right_toRightOf(imageView.getId(),getId());
        begin.Top_toTopOf(imageView.getId(),getId());
        begin.Bottom_toBottomOf(imageView.getId(),getId());
        begin.setDimensionRatio(imageView.getId(),ratio);

        begin.commit();

        container.setBitmapResource(bitmap);
    }

    /**
     * 设置图片drawable
     * @param drawable
     */
    public void setImageViewDrawable(Drawable drawable){
        imageView.setImageDrawable(drawable);
        Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        String ratio  = bitmap.getWidth()+":"+bitmap.getHeight();

        ConstraintUtil.ConstraintBegin begin = new ConstraintUtil(this).begin();

        begin.Left_toLeftOf(imageView.getId(),getId());
        begin.Right_toRightOf(imageView.getId(),getId());
        begin.Top_toTopOf(imageView.getId(),getId());
        begin.Bottom_toBottomOf(imageView.getId(),getId());
        begin.setDimensionRatio(imageView.getId(),ratio);

        begin.commit();

        container.setBitmapResource(bitmap);
    }
}
